package database;

import jdbc.JdbcDao;

/**
 * A pure static class used for build/delete database
 * @author CJLD
 *
 */
public class Builder {
	
	static JdbcDao mysql;
	
	static public void deleteAll() {
		mysql.excuteCommand("drop database " + mysql.DATABASENAME);
	}
	
	static public void createDatabase() {
		mysql.excuteCommand("create database " + mysql.DATABASENAME);
		
		mysql.excuteCommand("use " + mysql.DATABASENAME);
		
		/* create users table */
		String cmd = 
				"create table users (" +
						"email 				VARCHAR(50) UNIQUE KEY, " + 
						"admin 				ENUM('admin', 'user', 'forbidden', 'invalid'), " + 
						"realname 			VARCHAR(20), " +
						"birthdate 			DATE, "	+	
						"nickname 			VARCHAR(30), " +
						"gender 			ENUM('male', 'female'), " +
						"phonenumber 		VARCHAR(20), " +
						"emercont 			VARCHAR(20), " + 
						"emercontnum 		VARCHAR(20), " + 
						"address 			VARCHAR(40), " + 
						"qq 				VARCHAR(20), " + 
						"password 			VARCHAR(20), " + 
						"lastlogin			DATETIME, " +
						"session			VARCHAR(128), " + 
						"classlist 			VARCHAR(2048), " +
						"validcode			VARCHAR(20)) DEFAULT CHARSET=utf8;";
		mysql.excuteCommand(cmd);
		
		/* create classes table */
		cmd =
				"CREATE TABLE classes (" +
						"number 			VARCHAR(20) UNIQUE KEY, " + 
						"name 				VARCHAR(50), " + 
						"credit 			VARCHAR(5), " + 
						"teacher 			VARCHAR(30), " + 
						"numlimit 			INT, " + 
						"tempnum			INT, " +
						"studentlist 		VARCHAR(2048), " +
						"classdate 			DATETIME) DEFAULT CHARSET=utf8;";
		mysql.excuteCommand(cmd);

		/* create article table and contain comment */
		cmd =  
				"CREATE TABLE articles (" +
						"articleid 			INT unsigned not null auto_increment UNIQUE KEY, " + 
						"title 				VARCHAR(100), " + 
						"time 				DATETIME, " + 
						"authoremail		VARCHAR(50), " +
						"tags 				VARCHAR(100), " +
						"content			TEXT, " +
						/* not null if is a comment */
						"commentto			INT unsigned) DEFAULT CHARSET=utf8;";
		mysql.excuteCommand(cmd);
	}
	
	static public void main(String[] argv) {
		mysql = new JdbcDao();
		deleteAll();
		createDatabase();
	}
}
